package bma.common.langutil.core;

/**
 * 分页器
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 */
public class Pager {

	private int total;

	private int totalPage;

	private int pageSize;

	private int currentPage;

	public Pager() {

	}

	public Pager(int total, int curPage, int pageSize) {
		create(total, curPage, pageSize);
	}

	public void create(int total, int curPage, int pageSize) {
		this.total = total;
		totalPage = 0;
		this.pageSize = pageSize;
		this.currentPage = curPage;
		count();
	}

	private void count() {
		if (pageSize <= 0)
			pageSize = 20;
		totalPage = total / pageSize;
		if (total % pageSize > 0)
			totalPage++;

		if (currentPage <= 0)
			currentPage = 1;
		if (currentPage > totalPage)
			currentPage = totalPage;
	}

	public int getTotal() {
		return total;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int curPage) {
		this.currentPage = curPage;
		count();
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		count();
	}

	public void setTotal(int total) {
		this.total = total;
		count();
	}

	public int getStart() {
		return (currentPage > 1) ? (currentPage - 1) * pageSize : 0;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getNextPage() {
		return currentPage + 1 <= totalPage ? currentPage + 1 : totalPage;
	}

	public int getPrevPage() {
		return currentPage == 1 ? 1 : currentPage - 1;
	}

	public int getMorePrevPage() {
		return currentPage - 1;
	}

	public int getMoreNextPage() {
		return totalPage - currentPage;
	}
	
	@Override
	public String toString() {
		return ToStringUtil.fieldReflect(this);
	}
}